package my;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.app.car.R;
import com.app.car.util.SoftKeyboardUtil;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.flyco.dialog.widget.NormalListDialog;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;

import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;


/**
 * Created by zhengyy on 2017/7/27.
 */

public class DialogUtils {

     int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    public static NormalDialog DialogTwo(Context context, String title, String msg,
                                           String positiveButtonText, String negativeButtonText,
                                           final OnBtnClickL listenerOk, final OnBtnClickL listenerCancel) {

        final NormalDialog dialog = new NormalDialog(context);

        if (!TextUtils.isEmpty(title)) {
            dialog.isTitleShow(true);
            dialog.title(title);
            dialog.contentGravity(Gravity.CENTER);
        } else {
            dialog.isTitleShow(false);
        }
        dialog.content(msg);
        dialog.btnNum(2).btnText(positiveButtonText, negativeButtonText);
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {

                if (listenerOk != null) {
                    listenerOk.onBtnClick();
                }
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                if (listenerCancel != null) {
                    listenerCancel.onBtnClick();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;

    }

    public static NormalDialog DialogOne(Context context, String title, String msg, String btntext, final OnBtnClickL listener) {
        final NormalDialog dialog = new NormalDialog(context);
        if (!TextUtils.isEmpty(title)) {
            dialog.isTitleShow(true);
            dialog.title(title);
        } else {
            dialog.isTitleShow(false);
        }
        dialog.content(msg);
        dialog.btnNum(1).btnText(btntext);
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                if (listener != null) {
                    listener.onBtnClick();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }


    public static NormalDialog DialogOkMsg(Context context, String msg, final OnBtnClickL listener) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.isTitleShow(false);
        dialog.content(msg);
        dialog.btnNum(1).btnText(context.getString(R.string.app_ok));
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                if (listener != null) {
                    listener.onBtnClick();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }
    public static NormalDialog DialogOkMsg(Context context, String msg) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.isTitleShow(false);
        dialog.content(msg);
        dialog.btnNum(1).btnText(context.getString(R.string.app_ok));
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    public static NormalDialog DialogOKmsgFinish(final Activity context,
                                                   String msg) {

        final NormalDialog dialog = new NormalDialog(context);
        dialog.isTitleShow(false);
        dialog.content(msg);
        dialog.btnNum(1).btnText(context.getString(R.string.app_ok));
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                context.finish();
            }
        });
        dialog.show();
        return dialog;
    }

    public static NormalListDialog DialogStringList(final Activity mContext, String[] items, final OnOperItemClickL onOperItemClickL) {
        final NormalListDialog dialog = new NormalListDialog(mContext, items);
        dialog.title("请选择")//
                .layoutAnimation(null)
                .setOnOperItemClickL(new OnOperItemClickL() {
                    @Override
                    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                        onOperItemClickL.onOperItemClick(parent, view, position, id);
                        dialog.dismiss();
                    }
                });
        dialog.show(); 
        return dialog;
    }

    static QMUIPopup mNormalPopup = null;
    public static QMUIPopup QMUIPopuptop(Context mContext, View view,ArrayList<String> items, AdapterView.OnItemClickListener itemClickListener){
        ArrayAdapter adapter = new ArrayAdapter<>(mContext, R.layout.base_simple_list_item, items);
        AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(itemClickListener != null){
                    itemClickListener.onItemClick(adapterView,view,i,l);
                }
                if (mNormalPopup != null) {
                    mNormalPopup.dismiss();
                }
            }
        };
        mNormalPopup = QMUIPopups.listPopup(mContext,
                QMUIDisplayHelper.dp2px(mContext, 150),
                QMUIDisplayHelper.dp2px(mContext, 300),
                adapter,
                onItemClickListener)
                .animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
                .preferredDirection(QMUIPopup.DIRECTION_TOP)
                .shadow(true)
                .offsetYIfTop(QMUIDisplayHelper.dp2px(mContext, 5))
                .skinManager(QMUISkinManager.defaultInstance(mContext))
                .show(view);
        return mNormalPopup;
    }


    public static void showMessagePositiveDialog(Context mContext,String title,String message,String btnName,QMUIDialogAction.ActionListener actionListener) {
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setTitle(""+title)
                .setMessage(""+message)
                .setSkinManager(QMUISkinManager.defaultInstance(mContext))
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, ""+btnName, QMUIDialogAction.ACTION_PROP_POSITIVE,actionListener)
                .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }


    public static void showEditTextDialog(Activity activity,String title,String message,String actionName ,TextResult textResult) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(activity);
        builder.setTitle(title)
                .setSkinManager(QMUISkinManager.defaultInstance(activity))
                .setPlaceholder(message)
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        new Handler().postDelayed(new  Runnable(){
                            public  void  run() {
                                SoftKeyboardUtil.hideKeyboard(activity);
                            }
                        },  100 );
                    }
                })
                .addAction(actionName, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            textResult.resultText(text.toString());
                        }else {
                            textResult.resultText("1");
                        }
                        dialog.dismiss();
                        new Handler().postDelayed(new  Runnable(){
                            public  void  run() {
                                SoftKeyboardUtil.hideKeyboard(activity);
                            }
                        },  100 );
                    }
                })
                .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }




    public abstract static class TextResult {
        public abstract void resultText(String text);
    }



    public static void showWarningDialog(Context mContext,String connect,String confirmText,SweetAlertDialog.OnSweetClickListener listener){
        new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("提示")
                .setContentText(""+connect)
                .setCancelText("取消")
                .setConfirmText(""+confirmText)
                .showCancelButton(true)
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.dismiss();
                    }
                })
                .setConfirmClickListener(listener)
                .show();
    }

    public static void showWarningDialog(Context mContext,String connect){
        new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("提示！")
                .setContentText(""+connect)
                .show();
    }


    public static void showErrorDialog(Context mContext,String connect){
        new SweetAlertDialog(mContext, SweetAlertDialog.ERROR_TYPE)
                .setTitleText("提示！")
                .setContentText(""+connect)
                .show();
    }
    public static void showErrorDialog(Context mContext,String title,String connect){
        new SweetAlertDialog(mContext, SweetAlertDialog.ERROR_TYPE)
                .setTitleText(""+title)
                .setContentText(""+connect)
                .show();
    }

    public static void showSuccessrDialog(Context mContext,String title,String connect){
        new SweetAlertDialog(mContext, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText(""+title)
                .setContentText(""+connect)
                .show();
    }

    public static void showSuccessrDialog(SweetAlertDialog sDialog,String title,String connect,String confirmText){
        sDialog.setTitleText(""+title)
                .setContentText(""+connect)
                .setConfirmText(""+confirmText)
                .showCancelButton(false)
                .setCancelClickListener(null)
                .setConfirmClickListener(null)
                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
    }



}
